package com.astrofizzbizz.stoneedgegui.simbad;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.astrofizzbizz.stoneedgegui.app.AstroTarget;
import com.astrofizzbizz.stoneedgegui.app.StoneEdgeIIIGui;
import com.astrofizzbizz.stoneedgegui.utilities.StoneEdgeIIIException;


public class SimbadPanel extends JPanel {

	private static final long serialVersionUID = -4864968279503520456L;
	private String searchTxt = "Enter search terms here";
	private String pushButtonDisabledTxt = "No data to push";
	private String pushButtonEnabledTxt = "Set as target";
	private JTextField simbadSeekTxt;
	private JTextArea simbadRetArea;
	private JButton testButton;
	private JButton pushButton = new JButton(pushButtonDisabledTxt);
	private TargetInformation searchRes;
	private TargetResolver a = new TargetResolver();
	private StoneEdgeIIIGui stoneedgeIIIgui;
	private ActionListener simbadListener = new ActionListener() {
		public void actionPerformed(ActionEvent e)
		{
			// Submit search to SIMBAD, output the result, and enable
			// push to targetPanel
			String obj = simbadSeekTxt.getText();
			String retField;
			AstroTarget newTarget;
			try {
				searchRes = a.submit(obj);
				newTarget = new AstroTarget(searchRes.ident, searchRes.ra, searchRes.dec, stoneedgeIIIgui);
				retField = "Search term: " + obj + "\n";
				retField += "==================\n";
				retField += "SIMBAD Return Data\n";
				retField += "Identifier: " + searchRes.ident + "\n";
				retField += "RA: " + searchRes.ra + "\n";
				retField += "Dec: " + searchRes.dec + "\n";
				if (newTarget.isVisible()) {
					retField += "Target is currently visible.";
					enablePush2TargetPanel();
				} else {
					retField += "Target is not currently visible";
					disablePush2TargetPanel();
				}
				System.out.println(retField);
				simbadRetArea.setText(retField);
			} catch (SIMBADQueryException e1) {
				simbadRetArea.setText("Connection Failed");
				System.out.println("Connection failed");
				disablePush2TargetPanel();
			} catch (TargetNotFoundException e1) {
				simbadRetArea.setText(obj + ": Target not found");
				System.out.println(obj + ": Target not found");
				disablePush2TargetPanel();
			} catch (StoneEdgeIIIException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	};
	private ActionListener push2TargetListener = new ActionListener() {
		public void actionPerformed(ActionEvent e)
		{
			// Push the SIMBAD return object to the targetPanel, then switch control there
			String targetTabTitle = stoneedgeIIIgui.getTargetTabTitle();
			int targetTabIndex = stoneedgeIIIgui.getMainPane().indexOfTab(targetTabTitle);
			
			stoneedgeIIIgui.getTargetingPanel().setNewTarget(searchRes);
			stoneedgeIIIgui.getMainPane().setSelectedIndex(targetTabIndex);
		}
	};

	public SimbadPanel(StoneEdgeIIIGui stoneedgeIIIgui) {
		super();
		this.stoneedgeIIIgui = stoneedgeIIIgui;
		// A panel for searching, and another to push the data to targetPanel
		add(simbadSeekPanel());
		add(push2TargetPanel());
	}
	
	private JPanel push2TargetPanel() {
		JPanel push2TargetPanel = new JPanel(new GridBagLayout());
		
		// A button to push to the target panel.  Initially disabled.
		pushButton.addActionListener(push2TargetListener);
		disablePush2TargetPanel();
		push2TargetPanel.add(pushButton);
		
		return push2TargetPanel;
	}

	private void disablePush2TargetPanel() {
		// Bad (or no) data from SIMBAD, so disallow pushing to targetPanel
		pushButton.setText(pushButtonDisabledTxt);
		pushButton.setEnabled(false);
	}

	private void enablePush2TargetPanel() {
		// Good data from SIMBAD, so allow the user to push to targetPanel
		pushButton.setText(pushButtonEnabledTxt);
		pushButton.setEnabled(true);
	}

	private JPanel simbadSeekPanel() {
		JPanel simbadSeekPanel = new JPanel(new GridBagLayout());
		
		GridBagConstraints c = new GridBagConstraints();

		// We need a text entry box, a "search" button, and a text field
		// to print the output
		simbadSeekTxt = new JTextField(searchTxt, searchTxt.length());
		testButton = new JButton("Search");
		simbadRetArea = new JTextArea(10, searchTxt.length());
		simbadRetArea.setEditable(false);
		
		c.gridx = 0;
		c.gridy = 0;
		simbadSeekPanel.add(simbadSeekTxt, c);
		// Hitting "return" on the text box executes the same listener as the button
		simbadSeekTxt.addActionListener(simbadListener);

		c.gridx = 1;
		c.gridy = 0;
		simbadSeekPanel.add(testButton, c);
		testButton.addActionListener(simbadListener);

		c.gridx = 0;
		c.gridy = 1;
		c.gridwidth = 2;
		simbadSeekPanel.add(simbadRetArea, c);
		
		return simbadSeekPanel ;
	}
}
